<template>
     <view>
         <view class="content">
			<view class="login-box">
				
				<view v-if="!isLogin">
					<view class="login-txt">
						登录开启创造
					</view>
					<!-- <button type="primary" open-type="getUserInfo" @click="wxLogin">获取微信信息</button> -->
					<button style="background-color:rgb(244,212,32);font-size: 12px;font-family: 'Microsoft YaHei';font-weight: bold;" class="mini-btn" open-type="getPhoneNumber" type="default" size="mini" @getphonenumber="wxLogin">登录/注册</button>
				</view>
				<view v-else style="display: flex;">
					
					<view>
						<image class="headimg" :style="{ 'background': 'url(' + model.Picture + ')center' }"></image>
					</view>
					<view style="text-align: left;">
						<view style="color: #ffffff;font-size: 16px;">昵称: {{ model.FullName }}</view>
						<view><image src="@/static/images/icon.png" style="width:40rpx;margin-right:18rpx;vertical-align: middle;" mode="widthFix"></image><label style="color: #ffffff;font-size: 12px;">积分:{{model.Integral}}</label></view>
						<button style="margin-top:20rpx;background-color:rgb(244,212,32);font-size: 10px;font-family: 'Microsoft YaHei';font-weight: bold;" class="mini-btn" type="default" size="mini" @click="OutLogin">退出登录</button>
					</view>
					
				</view>

			</view>
		 </view>
        <view>
			<uni-list>
				<uni-list-item showArrow title="积分充值" :to="`./recharge?userid=11`"  />
				<uni-list-item showArrow to="../content/index?id=33" title="邀请注册"/>
				<uni-list-item showArrow to="../my/promotion" title="AI举荐官"/>
			</uni-list>
		</view>
		 
		 
     </view>
 </template>

<script>
import api from '@/common/api'
export default {
	data() {
		return {
			items: ['类别名称1', '类别名称2', '类别名称3'],
			api:api,
			  parameter:{page:1,pagesize:6},
			  current: 0,
			  openid:'',
			  aid:0,
			  isLogin: false,
			  status: 'more',
			  isMore: true,
			  unionid:'',
			  model:{},
			  contentText: {
			  	contentdown: '上拉加载更多',
			  	contentrefresh: '加载中',
			  	contentnomore: '没有更多'
			  }
		}
	},
	onLoad() {
		this.check()
		this.GetOpenID()
	},
	onShareAppMessage: function () {
		return this.share()
	},
	onShareTimeline:function (){
		return this.share()
	},
	onReachBottom() {
		
	},
	onPullDownRefresh() {

	},
	methods: {
		showshareabout(){
			uni.showModal({
				title: '提示',
				content: '点击右上角“...”分享给朋友注册！',
				showCancel: false
			});
		},
		share(){
			var fid = ''
			if(this.model&&this.model.UserID){
				fid = this.model.UserID
				console.log('fid',fid)
			}
			return {
				title:"AI侠",
				desc:'AI侠',
				query:'fid='+fid,
				path:'/pages/index/index?fid='+fid
			}
		},
		check(){
			var m = this.$storage.getJson('userinfo')
			if (m) {
				this.isLogin = true
				this.model = m
			} else {
				this.isLogin = false
				this.model = {}
			}
		},
		onClickItem(e) {
						if (this.current !== e.currentIndex) {
							this.current = e.currentIndex
						}
					},
	    search(e){
			   this.parameter.page = 1
			   this.projectList = []
			   this.getData();
		   },
		   OwnerShow(record){
			   this.$storage.setJson('project', record)
			   uni.switchTab({
			   	url:'/pages/owner/index'
			   })
			
		   },
		   GetOpenID(){
				var that = this
				uni.login({
			      provider: 'weixin',
			      success: (res) => {
			         let code = res.code
					 that.$request.post(that.$api.ai.getOpneID, {
							code: code
						},'form').then(res => {
							if (res.code===0) {
								this.openid = res.openid
								this.unionid=res.unionid
							}
							
						})			 
			      }
			  }) 
		   },
		   wxLogin: function(e) {
		   var that = this
		   let nickname = ''
		   let picture = ''
			let fid = this.$storage.getData('fid')
			if (!fid)
			{
				fid = 0
			}
			if (e.detail.errMsg == "getPhoneNumber:ok") {
				uni.getUserInfo({
				     provider: 'weixin',
				     success: function (infoRes) {
						console.log(infoRes)
						nickname =  infoRes.userInfo.nickName
						picture = infoRes.userInfo.avatarUrl
						
						that.$request.post(that.$api.ai.getinfo, {
							openid:that.openid,
							unionid:that.unionid,
							encrypted: e.detail.encryptedData,
							iv: e.detail.iv,
							fid,
							nickname,
							picture
						},'form').then(res => {
							if (res.code===0) {
								that.model = res.data
								that.isLogin = true
								that.$storage.setJson('userinfo', res.data)
								that.$storage.setData('token', that.openid);
								//that.$storage.setData('openid', that.openid);
							}
							
						})
						
				     }
				   })
				
			}
		   },
		   OutLogin(){
			   this.$storage.remove('userinfo')
			   this.$storage.remove('token')
			   this.isLogin=false
			   this.model = {}
		   },
		 // wxLogin: function(e) {
		 // 	console.log('e',e)
		 // 	var that = this
		 // 	let p1 = this.wxSilentLogin()
		 // 	let p2 = this.wxGetUserProfile()
		 // 	Promise.all([p1, p2]).then((res) => {
		 // 		console.log('res',res)
		 // 		let code = res[0]
		 // 		// 请求服务器
		 // 		that.$request.post(that.$api.ai.getcode, {
		 // 			code: code
		 // 		},'form').then(res => {
		 // 			console.log(res)
		 // 			that.openid = res.data
		 // 		})
		 // 	}).catch((err) => {
		 // 		console.log(err)
		 // 	})
		 // },
			phonelogin: function(e) {
				console.log('model',this.model, e)
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					this.$request.post(this.$api.ai.getinfo, {
						encrypted: e.detail.encryptedData,
						iv: e.detail.iv,
						openid: this.openid
					},'form').then(res => {
						console.log('res')
						this.$storage.setData('token', res.token);
						this.$storage.setJson('user', res.data);
						this.$storage.setData('online', '1');
						uni.navigateBack({
							delta: 1,
							success: function(e) {
								var page = getCurrentPages().pop();
								if (page == undefined || page == null) return;
								page.onLoad();
							}
						})
						// if(!res.mopenid || res.mopenid==''){
						// 	uni.navigateTo({
						// 	    url: '../login/mp'
						// 	});
						// }
						// else{
						// 	uni.navigateBack({
						// 		delta: 1,
						// 		success: function(e) {
						// 			var page = getCurrentPages().pop();
						// 			if (page == undefined || page == null) return;
						// 			page.onLoad();
						// 		}
						// 	})
						// }
						
					});
				} else {
					this.$request.post(this.$api.owner.phonelogin, {
						encrypted: e.detail.encryptedData,
						iv: e.detail.iv,
						name: this.model.name,
						picture: this.model.picture,
						province: this.model.province,
						city: this.model.city,
						district: this.model.district,
						sex: this.model.sex,
						openid: this.openid,
						unionid: this.model.unionid
					}).then(res => {
						
						this.$storage.setData('token', res.token);
						this.$storage.setJson('user', res.data);
						this.$storage.setData('online', '1');
						uni.navigateBack({
							delta: 1,
							success: function(e) {
								var page = getCurrentPages().pop();
								if (page == undefined || page == null) return;
								page.onLoad();
							}
						})
					});
				}
				
			},
		   getData:function(){
		   	this.status = 'loading';
		   	this.$request.postU(this.$api.project.list,this.parameter).then(res=>{
		   		if(res.data.length > 0) {
		   			
		   			this.projectList = this.projectList.concat(res.data)
		   			if(this.projectList.length < res.count) {
		   				this.status = 'more';
		   				this.isMore = true;
		   			} else {
		   				this.status = 'noMore';
		   				this.isMore = false;
		   			}
		   		} else {
		   			this.status = 'noMore';
		   			this.isMore = false;
		   		}
		   		
		   	});
		   }
		}
	}
</script>

<style>
 .content{
 }
.headimg{
		width: 5rem;
	    height: 5rem;
		margin-left: 50rpx;
	    border-radius: 50%;
	    margin-right: 50rpx;
	    background-position: center;

}
 .login-txt{
	 padding-top:70rpx;
	 margin-bottom: 10rpx;
	 font-size: 14px;
	 color:#FFFFFF
 }
 .uni-list-item__content-title {
 	color: #ffffff !important;
 }
 .uni-list-item {
	 
 	background-color: #272727 !important;
 }
 .login-box{
	 background-color: #101010;
	 height: 300rpx;
	 width: 100%;
	 text-align: center;
 }
 .line{height: 1px;background: #5d5c5c;}
page {
	    background-color: #272727;
	}
 .list-left{
	 wdith:200px;
	 margin-left:10px;
	 text-align:left;
	 height: 70px;
 }
 .list-right{
 	 margin-left:10px;
	 text-align:left;
	 height: 70px;
 }
 .search-ico-wapper{
     background-color: #FFFFFF;
     display: flex;
     flex-direction:column;
     justify-content: center;
     padding: 0upx 0upx 0upx 40upx;
     border-bottom-left-radius:18px;
     border-top-left-radius: 18px;
 }
 .search-ico-wapper1{
     background-color: #FFFFFF;
     display: flex;
     flex-direction:column;
     justify-content: center;
     padding: 0upx 40upx 0upx 0upx;
     border-bottom-right-radius:18px;
     border-top-right-radius: 18px;
 }
 .shadow{
     width: 638upx;
     height: 60upx;
     border-radius:18px;
     -moz-box-shadow:0 0 10px #e6e6e6;
     -webkit-box-shadow:0 0 10px #e6e6e6;
     box-shadow:0 0 10px #e6e6e6;
     position: relative;
     top: -90upx;
     left: 60upx;
 }
 </style>